package com.crm.web.controller;

import com.crm.entity.Svr;
import com.crm.service.SvrService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

@Controller
@RequestMapping("/plan")
public class PlanController {

    @Resource
    SvrService svrService;

    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(required = false, defaultValue = "1") Integer pageIndex,
                       @RequestParam(required = false, defaultValue = "") String svrCustName,
                       @RequestParam(required = false, defaultValue = "") String svrDueTo,
                       @RequestParam(required = false, defaultValue = "") String svrTitle) {
        Sort sort = new Sort(Sort.Direction.ASC, "svrId");
        Pageable pageable = PageRequest.of(pageIndex - 1, 3, sort);
        Page<Svr> svrPage = svrService.findAllByPage(svrCustName, svrDueTo, svrTitle, pageable);
        model.addAttribute("planPage", svrPage);
        model.addAttribute("svrCustName", svrCustName);
        model.addAttribute("svrDueTo", svrDueTo);
        model.addAttribute("svrTitle", svrTitle);
        return "plan/list";
    }
}
